package com.hh.system.action;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.hh.baseframework.util.check.Check;
import com.hh.baseframework.util.dto.ExtTree;
import com.hh.system.util.StaticVar;
import com.hh.system.util.SysParam;
import com.hh.system.util.base.BaseAction;
import com.hh.system.util.model.ClassJsModel;

@SuppressWarnings("serial")
public class ActionResourceFile extends BaseAction {
	private String path;

	public void js() throws IOException {
		String[] pathList = this.path.split(",");
		PrintWriter out = response.getWriter();
		response.setContentType("application/x-javascript");
		Long Last_Modified = 0l;
		StringBuffer stringBuffer = new StringBuffer();

		long clientLastModified = request.getDateHeader("If-Modified-Since");
		for (String path : pathList) {
			StringBuffer classCode = new StringBuffer();
			path = "page/" + path.replaceAll("\\.", "/") + ".class.js";
			ClassJsModel classJsModel = StaticVar.resource_js_map.get(path);
			URL url = org.springframework.util.ResourceUtils
					.getURL("classpath:" + path);
			String jarpath = url.getFile().split("\\!")[0];
			File jar = new File(new URL(jarpath).getPath());
			if (jar.exists()) {
				if (Last_Modified < jar.lastModified()) {
					Last_Modified = jar.lastModified();
				}
			}
			if (Check.isEmpty(classJsModel)
					|| classJsModel.getLastModified() < jar.lastModified()) {
				InputStream inputStream = url.openStream();
				BufferedReader in = new BufferedReader(new InputStreamReader(
						inputStream));
				String line = null;
				while ((line = in.readLine()) != null) {
					// out.println(line);
					classCode.append(line + "\n");
				}
				in.close();
				StaticVar.resource_js_map.put(
						path,
						new ClassJsModel(classCode.toString(), jar
								.lastModified()));
				stringBuffer.append(classCode);
			} else {
				stringBuffer.append(classJsModel.getCode());
			}
		}
		if (Math.abs(clientLastModified - Last_Modified) < 1000) {
			response.setStatus(304);
		} else {
			response.addDateHeader("Last-Modified", Last_Modified);
			response.setHeader("Cache-Control", "public");
			out.println(stringBuffer);
		}
		out.close();
	}

	public void queryIconFilePathList() {
		String filePath = "";
		if ("root".equals(request.getParameter("node"))) {
			filePath = path;
		} else {
			filePath = request.getParameter("node").toString();
		}
		String fileRealPath = StaticVar.webappPath
				+ (filePath.replace("common", SysParam.COMMON_PROJECT_NAME));
		File file = new File(fileRealPath);
		File[] subFile = file.listFiles();
		List<ExtTree> extTrees = new ArrayList<ExtTree>();
		for (File file2 : subFile) {
			ExtTree extTree = new ExtTree();
			extTree.setId(filePath + "/" + file2.getName());
			extTree.setText(filePath + "/" + file2.getName());
			if (!file2.isDirectory()) {
				extTree.setIcon(filePath + "/" + file2.getName());
				extTree.setLeaf(1);
			}
			extTrees.add(extTree);
		}
		this.returnResult(extTrees);
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	private void printIn(BufferedReader in) throws IOException {
		PrintWriter out = response.getWriter();
		response.setContentType("application/x-javascript");
		String line = null;
		while ((line = in.readLine()) != null) {
			out.println(line);
		}
		in.close();
		out.close();
	}

	public void json() throws IOException {
		InputStream in = getClass().getClassLoader().getResourceAsStream(path);
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		printIn(br);
	}
	// public void img() throws IOException {
	// response.setContentType("image/png");
	// InputStream in = getClass().getClassLoader().getResourceAsStream(path);
	// byte[] b = new byte[1024];
	// int len = -1;
	// OutputStream output = response.getOutputStream();
	// try {
	// while ((len = in.read(b)) > 0) {
	// output.write(b, 0, len);
	// }
	// } catch (IOException e) {
	// e.printStackTrace();
	// } finally {
	// output.flush();
	// output.close();
	// in.close();
	// }
	// }
}
